package com.stenio.fancat.controller.login;

import com.stenio.fancat.controller.Result;
import com.stenio.fancat.domain.user.UserLogin;
import com.stenio.fancat.exception.FanCatException;
import com.stenio.fancat.service.AnalysisEncryptedData;
import com.stenio.fancat.service.user.UserLoginService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.stenio.fancat.form.LoginForm;
import com.stenio.fancat.service.login.LoginService;
import com.stenio.fancat.vo.LoginVO;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 登录模块controller
 * @author stenio
 */
@RestController
public class LoginController {

	@Autowired
	private LoginService loginService;

	@Autowired
	private AnalysisEncryptedData analysisEncryptedData;

	@Autowired
	private UserLoginService userLoginService;

	/**
	 * 登录
	 * @param form
	 * @return
	 */
	@PostMapping("/login")
	public Result<Map<String,Object>> login(@RequestBody LoginForm form) {
		Result<Map<String,Object>> result = new Result<>();
		try {
			Map<String,Object> vo = loginService.login(form);
			result.setData(vo);
			result.setFlag(Result.FLAG.success);
			return result;
		} catch (FanCatException e) {
			result.setMsg(e.getMsg());
			result.setErrorCode(e.getErrorCode());
			result.setFlag(Result.FLAG.failure);
			return result;
		}
	}


	/**
	 * 获取openid
	 * @param loginForm
	 * @return
	 */
	@PostMapping(value = "/getopenId",produces = MediaType.APPLICATION_JSON_VALUE)
	public Result getopenId(@RequestBody LoginForm loginForm) {
		Result result = new Result();

		if(null == loginForm){
			result.setFlag(Result.FLAG.failure);
			return result;
		}
		JSONObject jsonObject = analysisEncryptedData.getOpenId(loginForm.getAppId(),loginForm.getCode(),loginForm.getSecret());
		result.setData(jsonObject);
		return result;
	}

	/**
	 * 获取解密之后的手机号
	 * @param loginForm
	 * @return
	 */
	@PostMapping(value = "/getPhone",produces = MediaType.APPLICATION_JSON_VALUE)
	public Result getPhone(@RequestBody LoginForm loginForm){

		Result result = new Result();

		if(null == loginForm){
			result.setFlag(Result.FLAG.failure);
			return result;
		}
		JSONObject jsonObject = analysisEncryptedData.AnalysisEncryptedData(loginForm.getEncryptedData(),loginForm.getSessionKey(),loginForm.getIv());
		result.setData(jsonObject);
		return result;

	}

	/**
	 * 管理后台登陆
	 * @param userLogin
	 * @return
	 */
	@PostMapping("/backgroundLogin")
	public Result backgroundLogin(@RequestBody UserLogin userLogin) {
		Result result = new Result();
		UserLogin userLogin1 = userLoginService.selectUserInfo(userLogin);
		if(userLogin1 == null) {
			result.setFlag(Result.FLAG.failure);
			result.setMsg("用户名密码错误");
			return result;
		}
		boolean token = true;
		result.setFlag(Result.FLAG.success);
		result.setData(token);
		return result;
	}

}
